const Koa = require('koa2');
const bodyParser = require('koa-bodyparser');
const Router = require('koa-router');
const config = require('./db/config.js');
const staticCache = require('koa-static-cache')
const cors = require('koa2-cors');
const router = new Router();
const app = new Koa();


app.use(cors());

app.use(bodyParser({
  formLimit: '1mb'
}))


app.use(async (ctx, next)=> {

  ctx.set('Access-Control-Allow-Origin', '*');
  ctx.set('Access-Control-Allow-Headers', 'Content-Type, Content-Length, Authorization, Accept, X-Requested-With , yourHeaderFeild');
  ctx.set('Access-Control-Allow-Methods', 'PUT, POST, GET, DELETE, OPTIONS');
  ctx.set("Access-Control-Allow-Credentials", true);
   if (ctx.method == 'OPTIONS') {
    ctx.body = 200;
  } else {
    await next();
  }
});

app.use(require('./routers/userinfo.js').routes())
app.use(require('./routers/login.js').routes())
app.use(require('./routers/reg.js').routes())
app.use(require('./routers/posts.js').routes())

app.listen(3000, function () {
    console.log('Server listening on port 3000');
});
